package net.xdclass.user.componet.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.common.exception.BizException;
import net.xdclass.user.model.MailDTO;
import net.xdclass.user.componet.MailService;
import net.xdclass.common.utils.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    /** 邮件发送方 */
    @Value("${spring.mail.username}")
    private String from;

    /** 邮件发送【简单邮件】 */
    @Override
    public void sendSimpleMail(MailDTO mail){
        try{
            checkMailEntity(mail);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(mail.getFrom()); // 发件人
            message.setSubject(mail.getSubject()); // 主题
            message.setText(mail.getText()); // 正文
            message.setTo(mail.getTo()); // 收件人
            if (mail.getSentDate() != null){ // 日期
                message.setSentDate(mail.getSentDate());
            }
            if (StringUtils.isNotEmpty(mail.getCc())){  // 抄送人
                message.setCc(mail.getCc());
            }
            javaMailSender.send(message);
        }catch (Exception e) {
            log.error("发送简单邮件发生错误：-> {}", e.getMessage());
        }
    }

    /** 邮件发送【带附件邮件】 */
    @Override
    public void sendMimeMail(MailDTO mail){
        try {
            checkMailEntity(mail);
            // true表示构建一个可以带附件的邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject(mail.getSubject());
            helper.setFrom(mail.getFrom());
            helper.setTo(mail.getTo());
            helper.setText(mail.getText());
            if (mail.getSentDate() != null){ // 日期
                helper.setSentDate(mail.getSentDate());
            }
            if (StringUtils.isNotEmpty(mail.getCc())){  // 抄送人
                helper.setCc(mail.getCc());
            }
            // 第一个参数是自定义的名称，后缀需要加上，第二个参数是文件的位置
            File file = new File(mail.getFilePath());
            log.info("邮件地址：-> {}", mail.getFilePath());
            log.info("邮件名称：-> {}", mail.getEnclosureName());
            helper.addAttachment(mail.getEnclosureName(), file);
            log.info("开始发送带附件的邮件。。。。");
            javaMailSender.send(mimeMessage);
            // 删除产生的文件
            FileUtils.deleteFile(mail.getFilePath());
        } catch (Exception e) {
            log.info("发送复杂邮件发生错误：-> {}", e.getMessage());
        }
    }

    /**
     * 验证邮箱信息
     * @param entity 邮箱信息类
     */
    private void checkMailEntity(MailDTO entity){
        if (entity == null) throw new BizException("发送邮件异常");
        entity.setFrom("XDCLASS" + "<" + from + ">");
        /** 验证必填项 */
        if (StringUtils.isEmpty(entity.getFrom())) throw new BizException("发件人不能为空");
        if (StringUtils.isEmpty(entity.getSubject())) throw new BizException("邮件主题不能为空");
        if (StringUtils.isEmpty(entity.getText())) throw new BizException("邮件正文不能为空");
        if (entity.getTo().length == 0) throw new BizException("收件人不能为空");
    }

}
